﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;
using System.Diagnostics;
using LogFileScanner.Properties;

using WeifenLuo.WinFormsUI.Docking;

namespace LogFileScanner
{
    public partial class StreamList : DockContent
    {
        private IList<Process> processes = new List<Process>();

        public StreamList()
        {
            InitializeComponent();
        }

        #region Public Interface

        public event Utility.VoidMethodDelegate NotifyProgressStart;
        public event Utility.VoidMethodDelegate NotifyProgressStop;
        public event Utility.MessageMethodDelegate NotifyLogMessage;

        public void Terminate()
        {
        }

        public void AddStreamViewer(StreamViewer viewer)
        {
            viewer.NotifyStreamClosed += new StreamViewer.StreamClosed(viewer_NotifyStreamClosed);

            ListViewItem item = new ListViewItem(viewer.FileToStream.FullName);
            item.SubItems.Add(viewer.Command);
            item.SubItems.Add(viewer.Arguments);
            item.Tag = viewer;

            listProcesses.Items.Add(item);
        }
        
        public void RemoveStreamViewer(StreamViewer viewer)
        {

            foreach (ListViewItem item in listProcesses.Items)
            {
                if (item.Tag == viewer)
                {
                    listProcesses.Items.Remove(item);
                    return;
                }
            }
        }

        public StreamViewer[] Streams
        {
            get
            {
                IList<StreamViewer> streams = new List<StreamViewer>();
                
                foreach (ListViewItem item in listProcesses.Items)
                {
                    streams.Add(item.Tag as StreamViewer);
                }

                return streams.ToArray<StreamViewer>();
            }
        }
        #endregion

        #region Event Callback

        private void processStart()
        {
            if (NotifyProgressStart != null)
            {
                NotifyProgressStart();
            }
        }

        private void processStop()
        {
            if (NotifyProgressStop != null)
            {
                NotifyProgressStop();
            }
        }


        private void logMessage(string value, Utility.MessageLevel level)
        {
            if (NotifyLogMessage != null)
            {
                NotifyLogMessage(value, level);
            }
        }

        #endregion 

        #region UI Event

        void viewer_NotifyStreamClosed(StreamViewer viewer)
        {
            RemoveStreamViewer(viewer);
        }

        private void StreamList_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            if (listProcesses.SelectedItems.Count > 0)
            {
                foreach (ListViewItem listItem in listProcesses.SelectedItems)
                {
                    StreamViewer viewer = listItem.Tag as StreamViewer;
                    if (viewer != null)
                    {
                        viewer.Terminate();
                    }
                }
            }
        }

        #endregion

    }
}
